//
//  CollectView.m
//  TextApplication
//
//  Created by dllo on 16/9/17.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "CollectView.h"
#import "SQLiteDataBaseManager.h"
#import "ImageUrl.h"

@interface CollectView ()
@property (nonatomic, strong) UIButton *collectButton;
@property (nonatomic, copy) void (^collect)(UIButton *collect);
@property (nonatomic, copy) void (^notCollect)(UIButton *notCollect);

@property (nonatomic, strong) SQLiteDataBaseManager *dataManager;

@property (nonatomic, strong) NSMutableArray <ImageUrl *>*dataArray;

@end

@implementation CollectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [NSMutableArray array];
        [self createCollectButton];
        _dataArray = [NSMutableArray array];
        _dataManager = [SQLiteDataBaseManager sharedManager];
        [_dataManager openSQLite3];
    }
    NSLog(@"11111111111111%@", self.imageID);

    return self;
}

- (void)createCollectButton {
    self.collectButton = [self setButtonWithImage:@"1" action:@selector(collectAction:)];
}

- (void)collectAction:(UIButton *)collectButton {
    
    
    [self.collectButton setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    [self.collectButton removeTarget:self action:@selector(collectAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.collectButton addTarget:self action:@selector(notCollectAction:) forControlEvents:UIControlEventTouchUpInside];
    self.collect(collectButton);
}

- (void)notCollectAction:(UIButton *)notCollectButton {
    [self.collectButton setImage:[UIImage imageNamed:@"32"] forState:UIControlStateNormal];
    [self.collectButton removeTarget:self action:@selector(notCollectAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.collectButton addTarget:self action:@selector(collectAction:) forControlEvents:UIControlEventTouchUpInside];
    self.notCollect(notCollectButton);
}

- (void)collect:(void (^)(UIButton *))collect notCollect:(void (^)(UIButton *))notCollect {
    self.collect = collect;
    self.notCollect = notCollect;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    NSLog(@"%@", self.imageID);
    self.dataArray = [_dataManager selectAll];
    NSLog(@"7777%@", self.dataArray[0].image_id);
        for (ImageUrl *url in self.dataArray) {
            if ([self.imageID isEqualToString:url.image_id]) {
                self.collectButton = [self setButtonWithImage:@"1" action:@selector(collectAction:)];
            } else {
            self.collectButton = [self setButtonWithImage:@"32" action:@selector(collectAction:)];
            }
        }
    
    
    self.collectButton.frame = self.bounds;
    self.collectButton.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
}

- (UIButton *)setButtonWithImage:(NSString *)imageName action:(SEL)action {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    NSLog(@"11111111111111%@", self.imageID);

    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    return button;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
